/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ReadXML;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Vector;
import org.kxml.Xml;
import org.kxml.parser.ParseEvent;
import org.kxml.parser.XmlParser;

public class ImageInfo {

    private static Vector vresultSign;
    private static Vector vresultText;
    private static Vector vsuggest;
    private static Vector vmean;
    private static Vector vanswer;
    private String test;

    public ImageInfo() {
        vresultSign = new Vector();
        vresultText = new Vector();
        vsuggest = new Vector();
        vmean = new Vector();
        vanswer = new Vector();
    }

    public void parsing() {
        // Start parsing.
        String url = "/xml/level1.xml";
        parse(url);
    }

    private void itemParsed(String resultSign, String resultText,
            String suggest, String mean, String[][] answer) {
        vresultSign.addElement(resultSign);
        vresultText.addElement(resultText);
        vsuggest.addElement(suggest);
        vmean.addElement(mean);
        vanswer.addElement(answer);
    }

    private void parse(final String url) {
        // set up the network connection
        InputStream fileInput = null;

        try {
            fileInput = getClass().getResourceAsStream(url);
            parsing(fileInput);
        } catch (IOException ioe) {
            exception(ioe);
        } finally {
            try {
                if (fileInput != null) {
                    fileInput.close();
                }
            } catch (IOException ignored) {
            }
        }
    }

// Blocking.
    private void parsing(InputStream in) throws IOException {
        Reader reader = new InputStreamReader(in, "UTF-8");
        XmlParser parser = new XmlParser(reader);
        ParseEvent pe = null;

        parser.skip();
        parser.read(Xml.START_TAG, null, "Level-1");

        boolean trucking = true;
        int i = 0, j = 0;

        String resultSign = null, resultText = null, suggest = null, mean = null;
        while (trucking) {
            pe = parser.read();
            if (pe.getType() == Xml.START_TAG) {
                String name = pe.getName();
                if (name.equals("Quesiton")) {
                    String[][] answer = new String[1][4];
                    while ((pe.getType() != Xml.END_TAG)
                            || (pe.getName().equals(name) == false)) {
                        pe = parser.read();
                        if (pe.getType() == Xml.START_TAG
                                && pe.getName().equals("ResultSign")) {
                            pe = parser.read();
                            resultSign = pe.getText();
                        } else if (pe.getType() == Xml.START_TAG
                                && pe.getName().equals("ResultText")) {
                            pe = parser.read();
                            resultText = pe.getText();
                        } else if (pe.getType() == Xml.START_TAG
                                && pe.getName().equals("Suggest")) {
                            pe = parser.read();
                            suggest = pe.getText();
                        } else if (pe.getType() == Xml.START_TAG
                                && pe.getName().equals("mean")) {
                            pe = parser.read();
                            mean = pe.getText();
                        } else if (pe.getType() == Xml.START_TAG
                                && pe.getName().equals("Answer")) {
                            while (pe.getType() != Xml.END_TAG || !pe.getName().equals("Answer")) {
                                pe = parser.read();
                                if (pe.getType() == Xml.START_TAG
                                        && pe.getName().equals("answertext")) {
                                    pe = parser.read();
                                    answer[0][j] = pe.getText();
                                    j++;
                                }
                            }
                        }
                    }
                    itemParsed(resultSign, resultText, suggest, mean, answer);
                    j = 0;
                    resultSign = null;
                    resultText = null;
                    suggest = null;
                    mean = null;
                    answer = null;
                    System.gc();
                } else {
                    while ((pe.getType() != Xml.END_TAG)
                            || (pe.getName().equals(name) == false)) {
                        pe = parser.read();
                    }
                }
            }
            if (pe.getType() == Xml.END_TAG
                    && pe.getName().equals("Level-1")) {
                trucking = false;
            }
        }
        reader = null;
        parser = null;
        pe = null;
        System.gc();
    }

    private void exception(IOException ioe) {
    }

    public static void cleanVector() {
        vanswer.removeAllElements();
        vanswer = null;
        vmean.removeAllElements();
        vmean = null;
        vresultSign.removeAllElements();
        vresultSign = null;
        vresultText.removeAllElements();
        vresultText = null;
        vsuggest.removeAllElements();
        vsuggest = null;
        System.gc();
    }

    public String[][] getVanswer(int i) {
        return (String[][]) vanswer.elementAt(i);
    }

    public String getVmean(int i) {
        return (String) vmean.elementAt(i);
    }

    public String getVresultSign(int i) {
        return (String) vresultSign.elementAt(i);
    }

    public String getVresultText(int i) {
        return (String) vresultText.elementAt(i);
    }

    public String getVsuggest(int i) {
        return (String) vsuggest.elementAt(i);
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}
